<?php

defined('SYS_RUN') or exit();

class Error {

    private static $_errors = array();

    private static $_bNoHtmlMode = false;

    private static $_bNoStackMode = false;

    public static function get_errors() {
        return self::$_errors;
    }

    public static function show_errors() {
        if(count(self::$_errors))
            foreach(self::$_errors as $s_error) echo $s_error;
    }

    public static function setNoHtmlMode() {
        self::$_bNoHtmlMode = true;
    }

    public static function setNoStackMode() {
        self::$_bNoStackMode = true;
    }

    public static function errorHandler($nErrorN, $sErrMsg, $sFileName, $nLinenum, $aVars = array()) {
        if(error_reporting() === 0) return false;

        if(is_object($nErrorN) && $nErrorN instanceof Exception) {
            $nLinenum = $nErrorN->getLine();
            $sErrMsg = $nErrorN->getMessage();
            $sFileName = $nErrorN->getFile();
            $nErrorN = $nErrorN->getCode();
        }

        if(self::$_bNoStackMode) {
            echo $sErrMsg;
            return false;
        }

        @ob_end_clean();
        @header('Content-Type: text/html');

        $s_err = "";

        if (substr(PHP_VERSION, 0, 1) < 5) {
            $i_start = 1;
        } else {
            $i_start = 0;
        }

        $b_no_html = false;
        if (self::$_bNoHtmlMode) {
            $b_no_html = true;
        }
        if (!$b_no_html) {
            $s_err .= '<table style="width: auto; border-top: 1px black solid; border-right: 1px black solid; margin: 4px 0px 4px 0px; font-size: 13px;">';
            $s_err .= '<tr><td colspan="3"><b>'.$sErrMsg.':</b> "'.$sFileName.'"('.$nLinenum.')</td></tr>';
        } else {
            $s_err .= $sErrMsg.'\n';
        }
        $a_files = debug_backtrace();
        for ($i=$i_start, $n=sizeof($a_files); $i<$n; ++$i) {
            if (!isset($a_files[$i]['file'])) {
                continue;
            }

            $sArgs = '';
            if (isset($a_files[$i]['args'])) {
                $a_args = array_merge($a_files[$i]['args'], array());
                if ($a_args and is_array($a_args)) {
                    foreach ($a_args as $k=>$v) {
                        if (is_numeric($v)) {
                            $a_args[$k] = '<span style="color:#7700AA">'.$v.'</span>';
                        } elseif(is_bool($v)) {
                            $a_args[$k] = '<span style="color:#222288;">'.($v ? 'TRUE' : 'FALSE').'</span>';
                        } elseif(is_null($v)) {
                            $a_args[$k] = '<span style="color:#222288;">NULL</span>';
                        } elseif(is_array($v)) {
                            $a_args[$k] = 'Array('.count($v).')';
                        } elseif (is_string($v) && ! (('"' == substr($v,0,1)) && ('"' == substr($v,-1)))) {
                            $a_args[$k] = '<span style="color:#0000FF">"'.$v.'"</span>';
                        } elseif(is_object($v)) {
                            unset($a_args[$k]);
                            $a_args[$k] = '{' . ucfirst(get_class($v)) . '}';
                        }
                    }
                }
                $sArgs = implode(', ', $a_args);
            }

            $s_func_name = (isset($a_files[$i]['class'])?$a_files[$i]['class']:'').
                (isset($a_files[$i]['type'])?$a_files[$i]['type']:'').
                $a_files[$i]['function'].'('.$sArgs.')';
            if ($i_start == $i) {
                $s_func_name = '<b>' . $s_func_name . '</b>';
            }
            $s_file = $a_files[$i]['file'];

            if ($b_no_html){
                $s_err .= "{$i}\t{$s_file}\t" . (isset($a_files[$i]['line']) ? $a_files[$i]['line'] : '') . "\t" . strip_tags($s_func_name) . "\n";
            } else {
                $s_err .= '<tr><td bgcolor="#DDEEFF" align="right">'.$i.'&nbsp;</td>'.
                    '<td style="border-bottom:1px #000 solid;">' . $s_file . '&nbsp;:&nbsp;<b>'.(isset($a_files[$i]['line']) ? $a_files[$i]['line'] : '').'</b>&nbsp;&nbsp; </td>'.
                    '<td style="border-bottom:1px #000 solid; border-left:1px #000 solid;">' .$s_func_name . '</td></tr>';
            }
        }

        if (!$b_no_html) {
            $s_err .= '</table>';
        }
        self::$_errors[] = $s_err;
        echo $s_err;
        exit(0);
    }

    public static function trigger($sMsg, $s_error_code = E_USER_WARNING) {
        trigger_error(strip_tags($sMsg), $s_error_code);
        return false;
    }

}